// // 引入 eventBus 包文件
// import 'package:event_bus/event_bus.dart';

// // 创建EventBus
// EventBus eventBus = new EventBus();

import 'dart:async';

import 'package:event_bus/event_bus.dart';

// // event 监听
// class EventFn {
//   // 想要接收的数据时什么类型的，就定义相同类型的变量
//   dynamic obj;
//   EventFn(this.obj);
// }

typedef void EventCallback<T>(T event);

class EventBusUtils {
  factory EventBusUtils() => _getInstance();
  static EventBusUtils get instance => _getInstance();
  static EventBusUtils _instance;
  EventBusUtils._internal() {
    // 初始化
    _eventBus = new EventBus();
  }

  //初始化eventBus
  EventBus _eventBus;

  static EventBusUtils _getInstance() {
    if (_instance == null) {
      _instance = new EventBusUtils._internal();
    }
    return _instance;
  }

  /// 开启eventbus订阅 并
  StreamSubscription on<T>(EventCallback<T> callback) {
    StreamSubscription stream = _eventBus.on<T>().listen((event) {
      callback(event);
    });
    // subscriptionList.add(stream);
    return stream;
  }

  /// 发送消息
  void emit(event) {
    _eventBus.fire(event);
  }

  /// 移除steam
  void off(StreamSubscription steam) {
    steam.cancel();
  }

  /// 移除steam
  void cancel() {
    _eventBus?.destroy();
  }
}

var eventBus = EventBusUtils.instance;
